package com.taptech.doufu.ui.view.topicview.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.cartoon.CartoonBeanListView;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class CartoonGridAdapter extends RecyclerView.Adapter<CartoonGridAdapter.ViewHolder>{

    private List<CartoonBeanListView> list;
    private Context context;
    public CartoonGridAdapter(Context context, List<CartoonBeanListView> list){
        this.list = list;
        this.context = context;
    }

    public CartoonBeanListView getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }

    public void refrush(List<CartoonBeanListView> list) {
        if (list != null) {
            this.list = list;
        }else {
            this.list.clear();
        }
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.cartoon_grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.title = (TextView) view.findViewById(R.id.title);
        holder.ivPic = (ImageView) view.findViewById(R.id.ivPic);
        holder.tvTag1 = (TextView) view.findViewById(R.id.tvTag1);
        holder.tvTag2 = (TextView) view.findViewById(R.id.tvTag2);
        holder.lay = view.findViewById(R.id.lay);
        int width =(ScreenUtil.getScreenWidth(context)-ScreenUtil.dip2px(context,8))/3-ScreenUtil.dip2px(context,8);
        int height = (int) (width*1.34f);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
        holder.ivPic.setLayoutParams(params);
        RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(width, RelativeLayout.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.BELOW,R.id.ivPic);
        titleParams.topMargin = ScreenUtil.dip2px(context, 10);
        holder.title.setLayoutParams(titleParams);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final CartoonBeanListView model = getItem(position);
        if (model != null) {
            holder.title.setText(model.getTitle());
            String tags = model.getTag_list();
            if (tags != null) {
                String[] items = tags.split(" ");
                if (items != null && items.length > 0) {
                    if (!TextUtils.isEmpty(items[0])){
                        holder.tvTag1.setVisibility(View.VISIBLE);
                        holder.tvTag1.setText(items[0]);
                    }else{
                        holder.tvTag1.setVisibility(View.GONE);
                    }
                    if (items.length > 1&& !TextUtils.isEmpty(items[1])) {
                        holder.tvTag2.setVisibility(View.VISIBLE);
                        holder.tvTag2.setText(items[1]);
                    }else{
                        holder.tvTag2.setVisibility(View.GONE);
                    }
                }else{
                    holder.tvTag1.setVisibility(View.GONE);
                    holder.tvTag2.setVisibility(View.GONE);
                }
            }else{
                holder.tvTag1.setVisibility(View.GONE);
                holder.tvTag2.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(model.getImage())) {
                GlideUtil.displayRoundImage(context, holder.ivPic, model.getImage(),3,R.drawable.img_default_loading_l);
            }
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CartoonServices.clickContentCard(context,model.getId(),Integer.parseInt(model.getObject_type()),model.getTarget_url(),model.getTitle());

                }
            });
            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title,tvTag1,tvTag2;
        ImageView ivPic;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_1));
        viewHolder.ivPic.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
    }
}
